<?php
/**
 * 商品分类统计商品数量
 * Enter description here ...
 * @author nijn
 *
 */
class GoodsTypeCount extends CActiveRecordBehavior{

	/**
	 * 商品分类模型
	 * Enter description here ...
	 * @var unknown_type
	 */
	public $goodsTypeModel;
	/**
	 * 统计字段
	 * Enter description here ...
	 * @var unknown_type
	 */
	public $countAttr;
	/**
	 * 商品分类字段
	 * Enter description here ...
	 * @var unknown_type
	 */
	public $cateAttr;
	/**
	 * 状态字段
	 * Enter description here ...
	 * @var array()
	 */
	public $statusArr = array(
		array(
			'attribute' => '',
			'status' => '',
		)
	);
	
	public $oldCateId;
	public $oldStatusAble = true;
	
	public function afterFind($event) {
		$owner = $this->getOwner();
		$this->oldCateId = $owner->{$this->cateAttr};
		foreach ($this->statusArr as $one) {
			$this->oldStatusAble &= $owner->{$one['attribute']} == $one['status'];
		}
	}
	
	
	public function beforeSave($event) {
		
		$owner = $this->getOwner();
		if($this->updateAbleByStatus() === 2 || ($owner->{$this->cateAttr} != $this->oldCateId && $this->oldStatusAble)) {
			$cateId = $this->oldCateId;
			$cateModel = new $this->goodsTypeModel;
			$cateModel = $cateModel->findByPk($cateId);
			if (is_object($cateModel)) {
				$ancestors = $cateModel->getAncestors();
				foreach($ancestors as $one) {
					if($one->{$this->countAttr} == 0) continue;
					$one->updateCounters(array($this->countAttr=>-1), "id='{$one->id}'");
				}
			}
		}
		
	}
	
	public function afterDelete($event) {
		if($this->oldStatusAble) {
			$cateId = $this->oldCateId;
			$cateModel = new $this->goodsTypeModel;
			$cateModel = $cateModel->findByPk($cateId);
			if (is_object($cateModel)) {
				$ancestors = $cateModel->getAncestors();
				foreach($ancestors as $one) {
					if($one->{$this->countAttr} == 0) continue;
					$one->updateCounters(array($this->countAttr=>-1), "id='{$one->id}'");
				}
			}
		}
	}
	

	public function afterSave($event) {
		$owner = $this->getOwner();
		
		if($this->updateAbleByStatus() === 1 || ($owner->getIsNewRecord() && $this->oldStatusAble)|| ($owner->{$this->cateAttr} != $this->oldCateId && $this->oldStatusAble)) {
			$cateId = $owner->{$this->cateAttr};
			$cateModel = new $this->goodsTypeModel;
			$cateModel = $cateModel->findByPk($cateId);
			if (is_object($cateModel)) {
				$ancestors = $cateModel->getAncestors();
				foreach($ancestors as $one) {
					$one->updateCounters(array($this->countAttr=>1), "id='{$one->id}'");
				}
			}
		}
		
	}
	
	/**
	 * 根据状态是否修改
	 * Enter description here ...
	 */
	protected function updateAbleByStatus() {
		$owner = $this->getOwner();
		if ($owner->getIsNewRecord()) {
			foreach ($this->statusArr as $one) {
				$this->oldStatusAble &= $owner->{$one['attribute']} == $one['status'];
			}
		}
		if ($this->oldStatusAble == 1 && $this->newStatusAble == 0) {
			return 2;//减
		} else if($this->oldStatusAble == 0 && $this->newStatusAble == 1) {
			return 1;//加
		}
		return 0;//不变
	}
	
	public function getNewStatusAble() {
		$owner = $this->getOwner();
		$statusAble = true;
		foreach ($this->statusArr as $one) {
			$statusAble &= $owner->{$one['attribute']} == $one['status'];
		}
		return $statusAble;
	}
}

?>